﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Cubes.Adapters
{
	public abstract class BaseAdapter
	{
		public const string NOT_SUPPORTED = "NOT_SUPPORTED";
		private DbProviderFactory _factory;

		#region Properties

		public string Database { get; set; }

		#endregion

		#region Constructors
		public BaseAdapter(string database)
		{
			this.Database = database;
		}
		#endregion

		#region Query Ajustments
		public virtual string Inspect(string sqlCommand)
		{
			return sqlCommand;
		}

		public virtual string AddOffsetLimit(string sqlCommand, int offset, int limit)
		{
			return NOT_SUPPORTED;
		}
		#endregion

		#region Abstracts
		protected abstract string AdoProvider { get; }

		protected abstract string BuildConnectionString();
		#endregion

		#region CreateConnection
		public virtual DbConnection CreateConnection()
		{

			DbConnection connection = this.Factory.CreateConnection();
			connection.ConnectionString = this.BuildConnectionString();
			return connection;
		}
		#endregion

		#region GetFactory
		public DbProviderFactory Factory
		{
			get
			{
				if (this._factory == null)
				{
					this._factory = DbProviderFactories.GetFactory(this.AdoProvider);
				}
				return this._factory;
			}
		}
		#endregion
	}
}
